package com.gss.plugin.oauth2.exception;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.gss.entity.R;
import com.gss.entity.enums.ConstantEnum;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

/**
 * @author DYang
 * @date 2023/7/21
 * oauth2 异常序列化
 */
@Slf4j
public class OAuth2ExceptionSerializer extends StdSerializer<OAuth2TransException> {

    public OAuth2ExceptionSerializer() {
        super(OAuth2TransException.class);
    }

    @Override
    public void serialize(OAuth2TransException e, JsonGenerator jGen, SerializerProvider provider) throws IOException {
        // 自定义返回格式内容
        R<Object> res = R.error(ConstantEnum.NON_AUTH);
        res.setMsg(e.getMessage());
        res.setDesc(e.getMyExtendMessage());

        jGen.writeObject(res);
    }

}
